class GroupController {
    constructor($cookieStore, $state, dialogService, GroupService, getCourseList, getGroupaliasList){

    	const vm = this;

        vm.$cookieStore      = $cookieStore;
        vm.$state            = $state;
        vm.dialogService     = dialogService;
        vm.GroupService      = GroupService;
        vm.getCourseList     = getCourseList;
        vm.getGroupaliasList = getGroupaliasList;

        // 科目列表
        vm.getCourseList && vm.getCourseList.code ==0 && (vm.courselist = vm.getCourseList.data);

        // 分组列表
        vm.getGroupaliasList && vm.getGroupaliasList.code ==0 && (vm.groupaliaslist = vm.getGroupaliasList.data);

        // 表单数据初始化
        vm.group = {
            group_title: '',
            group_intro: '',
            course: '',
            groupalias: ''
        }

    }


    // 表单提交
    submitForm(isValid) {

        const vm = this;

        // 验证失败, 提示用户
        if (!isValid) {
            if (!vm.group.course) {
                $.blink(angular.element(document.getElementsByName('course')), 5, 100)
            } else if (!vm.group.groupalias) {
                $.blink(angular.element(document.getElementsByName('groupalias')), 5, 100)
            } else if (!vm.group.group_title) {
                $.blink(angular.element(document.getElementsByName('groupTitle')), 5, 100)
            }
            return false;
        }

        // 验证通过
        if (isValid) {

            // 拼接POST数据格式
            const params = {
                group_title: vm.group.group_title,
                group_intro: vm.group.group_intro,
                course_id: vm.group.course.course_id,
                group_name: vm.group.groupalias.group_name
            }

            console.log(params)

            // 新增小组
            vm.GroupService.postGroupCreate(params, vm.$cookieStore.get('access_token'), vm.$state.params.race_id)
            .then( data => {

                // 成功调用
                data.code==0&&vm.dialogService.success('创建成功', 'dashboard.race.grouplist');

                // 调用失败, 提示用户
                (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                // 登录信息已过期, 请重新登录
                data.code==-1001&&vm.dialogService.error(data.message, true);

                console.log(data)
            }, error => console.log('error'));
        }

        return false;
    }

}

GroupController.$inject = ['$cookieStore', '$state', 'dialogService', 'GroupService', 'getCourseList', 'getGroupaliasList']; 

export default GroupController;